package com.gl.ruanzhu.controller;


import com.alibaba.fastjson.JSON;
import com.gl.basis.common.pojo.Page;
//import com.gl.ruanzhu.config.UserClickLog;
//import com.gl.ruanzhu.config.UserSearchLog;
//import com.gl.ruanzhu.util.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @ResponseBody
    public Page<?> getScientificOrg(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        logger.info("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        logger.info("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String projectPath = new File("").getAbsolutePath();
        System.out.println("文件上传后的路径"+projectPath);
        //File.separator获取不同操作系统下的分隔符。
        File dest = new File(projectPath + File.separator  + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        Page<?> page=new Page<>();
        try {
            file.transferTo(dest);
            Map map =new HashMap();
            map.put("path",projectPath+ File.separator);
            map.put("fileName",fileName);
            page.setMsg("上传成功");
            page.setMap(map);
            return  page;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        page.setMsg("上传失败");
        return page;
    }
}
